<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Chat/classes/PhpFox_ServiceChat.class.php, $Author:  $ */



/** Service for working with chat functionality (all types)
 *
 * @package module.Chat
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Chat_ServiceChat extends BaseService
{
    function getCaller($sCurUser)
    {
        $oChat = &$this->_oModule->getItem('Chat');

        $sSql = 'SELECT * FROM '.App::getT('messenger_id').' WHERE `check` != "2" AND user2 = "'.$sCurUser.'" ORDER BY time DESC';
        $aRes = $oChat->oDb->getRows($sSql);
        foreach ($aRes as $i=>$aData)
        {
            if(time()-$aData['time'] > App::getParam('chat.wait'))
            {
                $oChat->oDb->query('DELETE FROM '.$oChat->sTable.' WHERE id = '.$aData['id']);
                unset($aRes[$i]);
            }
            if(!empty($aData))
            {
                $oChat->aData['seen'] = 1;
                $oChat->aData['id'] = $aData['id'];
                $oChat->update();
            }
        }

        return $aRes;
    }

    function makeMessenger($sCaller, $sRecepient)
    {
        $oChat = &$this->_oModule->getItem('Chat');

        $oChat->aData['user1']  = $sCaller;
        $oChat->aData['user2']  = $sRecepient;
        $oChat->aData['check']  = 0;
        $oChat->aData['time']   = time();
        $oChat->aData['seen']   = 0;

        $oChat->oDb->insert(App::getT('messenger_id'), $oChat->aData);
        return $oChat->oDb->getLastID();
    }

    function checkStatus($iChatId)
    {
        $oChat = &$this->_oModule->getItem('Chat');

        $sSql = 'SELECT `check`, `time` FROM '.App::getT('messenger_id').' WHERE id = '.$iChatId;

        $aRes = $oChat->oDb->getRow($sSql);
        $aUser = $this->getUsers($iChatId);
        if(time()-intval($aRes['time']) > App::getParam('chat.wait'))
        {
            $this->addErrors(App::format('Mod_Chat.waiting.expired'));
            return false;
        }
        if($aRes['check'] == 2)
        {
            $this->addErrors(App::format('Mod_Chat.waiting.denied', array($aUser['user2'])));
            return false;
        }
        if($aRes['check'] == 1)
        {
            return false;
        }

        return true;
    }

    function checkMessenger($sCurUser)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $sSql = 'SELECT user1 FROM '.App::getT('messenger_id').' WHERE user2 = "'.$sCurUser.'" AND seen = 0';

        return $oChat->oDb->getRows($sSql);
    }

    function saveChatMessage($sCurrentUser, $sText, $iChatId)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('messenger');
        $oChat->aData['message'] = mysql_escape_string($sText);
        $oChat->aData['post_time'] = $oChat->oDb->date();
        $oChat->aData['user_name']   = $sCurrentUser;
        $oChat->aData['chat_id'] = $iChatId;

        $oChat->insert();
        return true;
    }

    function getChatMessage($iChatId, $iLastMessageId=0)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('messenger');

        $sSql = 'SELECT message_id, user_name, message, post_time FROM '.$oChat->sTable.' WHERE chat_id = '.$iChatId.' AND message_id > '.$iLastMessageId.'';
        return $oChat->oDb->getRows($sSql);
    }

    function setChatStatus($iChatId, $iStatus = 0)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->aData['check'] = $iStatus;
        $oChat->aData['id']    = $iChatId;
        $oChat->update();
        return true;
    }

    function getUsers($iChatId)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        return $oChat->oDb->getRow('SELECT user1, user2 FROM '.$oChat->sTable.' WHERE id = '.$iChatId);
    }

    function setUserSession($sUser, $iChatId)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('messenger_check');
        $oChat->oDb->query('DELETE FROM '.$oChat->sTable.'
                                    WHERE `user` = "'.$sUser.'" AND `time` > '.intval(time()-10));

        $oChat->aData['user']       = $sUser;
        $oChat->aData['chat_id']    = $iChatId;
        $oChat->aData['time']       = time();

        $oChat->insert();
        return true;
    }

    function getCurrentSession($iChatId)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('messenger_check');
        $iTime = time();

        $aSess = $oChat->oDb->getRows('SELECT `user`,`time`
                                        FROM '.$oChat->sTable.'
                                        WHERE chat_id = '.$iChatId.'
                                        GROUP BY `user`
                                        ORDER BY `time` DESC');

        if(empty($aSess))
        {
            $oChat->oDb->query('DELETE FROM '.$oChat->sTable.' WHERE chat_id = '.$iChatId);
            return false;
        }

        foreach ($aSess as $i=>$aDate)
        {
            if(intval($aDate['time'])+5 < $iTime)
            {
                $oChat->oDb->query('DELETE FROM '.$oChat->sTable.' WHERE chat_id = '.$iChatId);
                return false;
            }
        }
        return true;
    }

    function checkCurrentChat($sCaller, $sRecepient)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->oDb->query('DELETE FROM '.$oChat->sTable.' WHERE user1 = "'.$sCaller.'" AND user2 = "'.$sRecepient.'" AND `time` < '.intval(time()-App::getParam('chat.wait')));
        $sSql = 'SELECT `id`
                    FROM '.$oChat->sTable.'
                    WHERE user1= "'.$sCaller.'" AND user2 = "'.$sRecepient.'" AND `time` > '.intval(time()-App::getParam('chat.wait'));
        $aRes = $oChat->oDb->getRows($sSql);

        return (!empty($aRes)) ? false : true;
    }

    function checkUserStatusYShout($sUser)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('chat_online');

        $sSql = 'SELECT `id` FROM '.$oChat->sTable.' WHERE `user`= "'.$sUser.'"';
        return $oChat->oDb->getField($sSql);
    }

    function updateUserOnlineYShout ($sUser, $iUserId = 0)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('chat_online');

        $oChat->aData['user'] = $sUser;
        $oChat->aData['time'] = time();
        ($iUserId) ? $oChat->aData['id'] = $iUserId : null;
        (!$iUserId) ? $oChat->insert() : $oChat->update();
        return (!$iUserId) ? 0 : $iUserId;
    }

    function getUserOnlineYShout($iRoomId = 0)
    {
        $oChat = &$this->_oModule->getItem('Chat');
        $oChat->_initTable('chat_online');

        // $sSql = 'SELECT `user` FROM '.$oChat->sTable.' WHERE `time` > '.intval(time()-App::getParam('user_max_online'));
        $sSql = 'SELECT `user` FROM '.$oChat->sTable.' WHERE roomid = '. $iRoomId .'';
        // $sSql = "SELECT user FROM ". App::getT('user') ." ORDER BY user ASC";
        return $oChat->oDb->getRows($sSql);
    }
}
?>